package com.studyyoun.android.nonghe.person.activity;
/**
 * Created by zhaolong on 2017/10/5.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidlongs.pullrefreshrecyclerylib.common.PullRecyclerViewUtils;
import com.androidlongs.pullrefreshrecyclerylib.inter.PullRecyclerViewLinserner;
import com.androidlongs.pullrefreshrecyclerylib.inter.PullRecyclerViewOnItemClickLinserner;
import com.androidlongs.pullrefreshrecyclerylib.model.PullRecyclerMoreStatueModel;
import com.studyyoun.android.androidcommonbaselibrary.activity.CommonBaseActivity;
import com.studyyoun.android.androidcommonbaselibrary.glide.CommonGlideUtils;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.utils.DateUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.activity.VideoDetaileActivity;
import com.studyyoun.android.nonghe.home.model.VideoModel;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;
import com.studyyoun.android.nonghe.person.model.UserReadVideoModel;
import com.studyyoun.android.nonghe.welcom.CommonUserHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * class infation
 */
public class MyVideoReadHistoryActivity extends CommonBaseActivity {
    private View mBackLinearLayout;
    private TextView mHeaderTitleTextView;
    private LinearLayout mMainContentLinearLayout;
    private PullRecyclerViewUtils mPullRecyclerViewUtils;

    private List<PullRecyclerMoreStatueModel> mCommonReadHistoryModelList = new ArrayList<>();

    @Override
    protected void getAllIntentExtraDatas(Intent intent) {

    }

    @Override
    protected int getCommonLayoutId() {
        return R.layout.activity_my_read_history_layout;
    }

    @Override
    protected void commonInitView(View view) {
        mBackLinearLayout = findViewById(R.id.ll_base_back);
        mHeaderTitleTextView = findViewById(R.id.tv_base_header_title);
        mMainContentLinearLayout = findViewById(R.id.ll_verson_main_content);
        //初始化
        mPullRecyclerViewUtils = PullRecyclerViewUtils.getInstance();


        //3初始化Recyclerview
        /**
         * 参数一 this Context实例
         * 参数二 单一布局模式条目布局ID
         * 参数三 数据集合
         * 参数四 回调监听
         */
        RelativeLayout relativeLayout = mPullRecyclerViewUtils.setRecyclerViewFunction(mContext, -1, mCommonReadHistoryModelList, mPullRecclerLinserner, mItemClickLinserner);

        //4相关设置

        //下拉刷新和上拉加载更多模式
        mPullRecyclerViewUtils.setRecyclerviewStatue(PullRecyclerViewUtils.RECYCLRYVIEW_STATUE.PULL_AND_UP);

        mPullRecyclerViewUtils.setFirstDefaultPageType(PullRecyclerViewUtils.SHOW_DEFAUTLE_PAGE_TYPE.LOADING);

        //5将recyclerview添加到当前显示的页面中
        mMainContentLinearLayout.addView(relativeLayout);
    }

    @Override
    protected void commonFunction() {

    }

    @Override
    protected void commonDelayFunction() {
        mHeaderTitleTextView.setText("观看记录");
        mBackLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        loadingNetDataFunction();
    }

    private int pageIndex = 0;
    private int pageSize = 30;

    private void loadingNetDataFunction() {
        mPullRecyclerViewUtils.startLoadingAnimation();
        String url = HttpUrlHelper.sAddUserGetVideoRecodeFromIdUrl;
        ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();
        concurrentHashMap.put("pageIndex", String.valueOf(pageIndex));
        concurrentHashMap.put("pageSize", String.valueOf(pageSize));
        concurrentHashMap.put("userId", String.valueOf(CommonUserHelper.getUserModel().getUserId()));
        OkhttpCommonClient.sentPostRequest(url, concurrentHashMap, new OkhttpCommonCallBack(UserReadVideoModel.class) {
            @Override
            public void onSuccess(Object obj) {
                mPullRecyclerViewUtils.stopLoadingAnimation();
                List<UserReadVideoModel> list = (List<UserReadVideoModel>) obj;

                switch (mLoadingType) {
                    case normal:
                    case pullLoading:
                        break;
                }

                for (int i = 0; i < list.size(); i++) {
                    PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                    pullRecyclerMoreStatueModel.model = list.get(i);

                    pullRecyclerMoreStatueModel.itemType = 1;
                    pullRecyclerMoreStatueModel.itemLayoutId = R.layout.item_user_read_video_history_layout;

                    mCommonReadHistoryModelList.add(pullRecyclerMoreStatueModel);
                }

                switch (mLoadingType) {
                    case normal:
                    case pullLoading:
                        mPullRecyclerViewUtils.setLoadingDataList(mCommonReadHistoryModelList);
                        break;
                    case upLoading:
                        if (list.size() > 0) {
                            mPullRecyclerViewUtils.setLoadingDataList(mCommonReadHistoryModelList);
                        } else {
                            mPullRecyclerViewUtils.setLoadingDataList(null);
                        }
                        break;
                }

            }

            @Override
            public void onFaile(Object obj) {
                mPullRecyclerViewUtils.stopLoadingAnimation();
                mPullRecyclerViewUtils.setLoadingDataList(mCommonReadHistoryModelList);
            }
        });
    }

    private LoadingType mLoadingType = LoadingType.normal;

    private enum LoadingType {
        normal,
        upLoading,
        pullLoading
    }

    private PullRecyclerViewLinserner mPullRecclerLinserner = new PullRecyclerViewLinserner() {
        @Override
        public void loadMoreData() {

            pageIndex++;
            mLoadingType = LoadingType.upLoading;
            loadingNetDataFunction();
        }

        @Override
        public void loadingRefresDataFunction() {
            mCommonReadHistoryModelList.clear();
            pageIndex = 0;
            mLoadingType = LoadingType.pullLoading;
            loadingNetDataFunction();
        }

        @Override
        public void setViewDatas(View itemView, int position, int itemType, Object object) {
            PullRecyclerMoreStatueModel moreStatueModel = (PullRecyclerMoreStatueModel) object;
            int itemType1 = moreStatueModel.itemType;

            if (itemType1 == 1) {

                UserReadVideoModel readVideoModel = (UserReadVideoModel) moreStatueModel.model;

                TextView artNameTextView = itemView.findViewById(R.id.tv_user_read_art_name);
                TextView artReadDateTextView = itemView.findViewById(R.id.tv_user_read_art_date);

                List<VideoModel> videoModels = readVideoModel.getVideoModels();
                if (videoModels != null && videoModels.size() > 0) {
                    VideoModel videoModel = videoModels.get(0);
                    if (videoModel != null) {
                        String artName = videoModel.getTitle();
                        artNameTextView.setText(artName);

                        ImageView artHeaderImageView = itemView.findViewById(R.id.iv_user_read_art_title);
                        String imageUrl = HttpUrlHelper.BaseImageHost + videoModel.getImageUrl();
                        CommonGlideUtils.showImageView(mContext, R.mipmap.default_pic, imageUrl, artHeaderImageView);
                    } else {
                        LogUtils.d("artModel is null");
                        artNameTextView.setText("***");
                    }
                } else {
                    LogUtils.d("artModels is null");
                    artNameTextView.setText("***");
                }



                ImageView headerImageView = itemView.findViewById(R.id.iv_user_read_art_header_img);

                if (position==0){
                    headerImageView.setVisibility(View.VISIBLE);
                    artReadDateTextView.setText(DateUtils.getInstance().formatTime(Long.valueOf(readVideoModel.getCreateTime()), DateUtils.DEFAULT_TIME_FORMAT2));

                }else {
                    if ((position - 1)>(mCommonReadHistoryModelList.size()-1)){
                        return;
                    }
                    PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = mCommonReadHistoryModelList.get(position - 1);
                    UserReadVideoModel preReadVideoModel = (UserReadVideoModel)pullRecyclerMoreStatueModel.model;
                    String preFormatime = DateUtils.getInstance().formatTime(Long.valueOf(preReadVideoModel.getCreateTime()), DateUtils.DEFAULT_DATE_FORMAT);
                    String currentFormatime = DateUtils.getInstance().formatTime(Long.valueOf(readVideoModel.getCreateTime()), DateUtils.DEFAULT_DATE_FORMAT);

                    if (TextUtils.equals(preFormatime,currentFormatime)) {
                        headerImageView.setVisibility(View.INVISIBLE);
                        artReadDateTextView.setText(DateUtils.getInstance().formatTime(Long.valueOf(readVideoModel.getCreateTime()), "HH : mm : ss"));

                    }else {
                        headerImageView.setVisibility(View.VISIBLE);
                        artReadDateTextView.setText(DateUtils.getInstance().formatTime(Long.valueOf(readVideoModel.getCreateTime()), DateUtils.DEFAULT_TIME_FORMAT2));

                    }
                }

            }

        }
    };

    private PullRecyclerViewOnItemClickLinserner mItemClickLinserner = new PullRecyclerViewOnItemClickLinserner() {
        @Override
        public void setonItemClick(int position, int itemType, Object object) {

            PullRecyclerMoreStatueModel moreStatueModel = (PullRecyclerMoreStatueModel) object;
            UserReadVideoModel userReadArtModel = (UserReadVideoModel) moreStatueModel.model;

            Intent intent = new Intent(MyVideoReadHistoryActivity.this, VideoDetaileActivity.class);
            intent.putExtra("videoId",String.valueOf(userReadArtModel.getVideoId()));
            MyVideoReadHistoryActivity.this.startActivity(intent);
        }
    };
}
